<template>
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.Machine.engineCode')" />
		<!-- 表单 -->
		<view class="formList">
			<zy-forms :model="formList" border labelWidth="180">
				<zy-form-item :label="$t('homepage.Machine.engineCode')+$t('public.colon')"><text
						class="formText">{{formList.code || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.Machine.engineName')+$t('public.colon')"><text
						class="formText">{{formList.name || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.Machine.engineSpec')+$t('public.colon')"><text
						class="formText">{{formList.spec || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.Machine.admissionTime')+$t('public.colon')"><text
						class="formText">{{formList.admissionDate || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.Machine.engineStatus')+$t('public.colon')"><text
						class="formText">{{formList.status || $t('public.null')}}</text></zy-form-item>
				<zy-form-item :label="$t('homepage.Machine.belong')+$t('public.colon')"><text
						class="formText">{{formList.deptName || $t('public.null')}}</text></zy-form-item>
				<zy-form-item :label="$t('homepage.Machine.departSelect')+$t('public.colon')" required v-show="formDepartShow">
					<uni-data-picker :placeholder="$t('homepage.Machine.selectDepart')"
						:popup-title="$t('public.pleaseSelect')" :localdata="departDatas" v-model="formList.depart"
						@change="onchange" @popupopened="onpopupopened">
					</uni-data-picker>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.Machine.allocationSituation')+$t('public.colon')"><text
						class="formText">{{formList.currentDeptName || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('public.remarkInfo')+$t('public.colon')"><text
						class="formText">{{formList.remarks || $t('public.null')}}</text></zy-form-item>
			</zy-forms>
		</view>
		<!-- 底部按钮 -->
		<zy-button-group class="zy_btn_group">
			<zy-button v-show="transferBtnShow" @click="transferOpen" :disabled="transferMachine" type="warning"
				class="btn">{{$t('homepage.Machine.tranfer')}}</zy-button>
			<zy-button v-show="scrapBtnShow" @click="scrapOpen" type="danger"
				class="btn">{{$t('homepage.Machine.scrap')}}</zy-button>
			<zy-button v-show="outBtnShow" @click="submitOpen" :disabled="submitMachine"
				class="btn">{{$t('homepage.Machine.borrowGive')}}
			</zy-button>
			<zy-button v-show="inBtnShow" @click="sureOpen" :disabled="sureMachine" type="primary"
				class="btn">{{$t('homepage.Machine.receive')}}
			</zy-button>
		</zy-button-group>
		<!-- 弹出层 -->
		<!-- 报废 -->
		<zy-popup ref="scrapPopup" :title="$t('homepage.Machine.engineReason')" type="dialog" @confirm="scrapConfirm">
			<zy-input :placeholder="$t('public.pleaseInput')+$t('homepage.Machine.engineReason')" focus
				v-model="scrapInput" border="bottom" style="margin: 20rpx 0;">
			</zy-input>
		</zy-popup>
		<!-- 接收 -->
		<zy-popup ref="surePopup" :title="$t('homepage.Machine.engineReceive')" type="dialog" @confirm="sureConfirm">
			<text style="margin: 20rpx 0;">{{$t('public.isSure')}}{{$t('homepage.Machine.engineReceive')}}{{$t('public.question')}}</text>
		</zy-popup>
		<!-- 借还 -->
		<zy-popup ref="submitPopup" :title="$t('homepage.Machine.engineBorrowReceive')" type="dialog"
			@confirm="submitConfirm">
			<radio-group @change="radioChange" style="display: flex;margin: 20rpx 0;width: 100%;">
				<label v-for="(item, index) in radioList" :key="item.value" style="width: 50%;text-align: center;">
					<view>
						<radio :value="item.name" :checked="index === 0" />
					</view>
					<view>{{item.label}}</view>
				</label>
			</radio-group>
		</zy-popup>
		<!-- 划拨 -->
		<zy-popup ref="transferPopup" :title="$t('homepage.Machine.engineTranfer')" type="dialog"
			@confirm="transferConfirm">
			<text style="margin: 20rpx 0;">{{$t('public.isSure')}}{{$t('homepage.Machine.engineTranfer')}}{{$t('public.question')}}</text>
		</zy-popup>
	</view>
</template>

<script>
	import {
		getEntityByCode,
		transfer,
		machineSubmit,
		confirmReceive,
		machineScrap,
		queryTreeList,
		getAppMenu
	} from "@/api/homepage.js"
	export default {
		data() {
			return {
				formList: {
					code: "",
					name: "",
					spec: "",
					admissionDate: "",
					status: "",
					deptName: "",
					depart: "",
					currentDeptName: "",
					remarks: "",
				},
				departDatas: [],
				formDepartShow: false,
				selectChildId: '',
				selectChildTitle: '',
				scrapInput: '',
				radioList: [{
						label: this.$t('homepage.Machine.borrowOut'),
						name: '0',
						disabled: false
					},
					{
						label: this.$t('homepage.Machine.give'),
						name: '1',
						disabled: false
					},
				],
				radio: 0,
				transferBtnShow: false,
				scrapBtnShow: false,
				outBtnShow: false,
				inBtnShow: false,
				transferMachine: false,
				submitMachine: false,
				sureMachine: false,
				datas: []
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			setTimeout(function() {
				uni.redirectTo({
					url: "/pages/HomePage/Machine/MachineManage/index"
				})
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onLoad() {
			//PDA扫描
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList: function(e) {
				getEntityByCode({
					code: e,
				}).then((res) => {
					if (res.code == 0) {
						this.getAppMenuFct()
						this.formList.code = res.data.code,
							this.formList.name = res.data.name,
							this.formList.spec = res.data.spec,
							this.formList.admissionDate = res.data.admissionDate
						if (res.data.status == 0) {
							this.formList.status = this.$t('homepage.Machine.using')
						} else if (res.data.status == 1) {
							this.formList.status = this.$t('homepage.Machine.unused')
						} else if (res.data.status == 2) {
							this.formList.status = this.$t('homepage.Machine.scrapReuse')
						} else if (res.data.status == 3) {
							this.formList.status = this.$t('homepage.Machine.stop')
						} else if (res.data.status == 4) {
							this.formList.status = this.$t('homepage.Machine.borrowed')
						} else if (res.data.status == 5) {
							this.formList.status = this.$t('homepage.Machine.unrepair')
						} else {
							this.formList.status = this.$t('homepage.Machine.untransfer')
						}
						this.formList.deptName = res.data.deptName
						if (res.data.currentDeptName == "") {
							this.formList.currentDeptName = this.$t('public.null')
						} else {
							this.formList.currentDeptName = res.data.currentDeptName
						}
						this.formList.remarks = res.data.remarks
						//划拨
						console.log(res.data.deptName)
						if (res.data.deptName == '' || res.data.deptName == null) {
							this.transferMachine = false
							this.formDepartShow = true
						} else {
							this.transferMachine = true
						}
						//借出归还
						if (res.data.deptName != '' && res.data.deptName != null) {
							this.submitMachine = false
						} else {
							this.submitMachine = true
						}
						//接收
						if (res.data.status == '4' && res.data.machineLoan != '' && res.data.machineLoan !=
							null && res.data.machineLoan.billType == '0') {
							this.sureMachine = false
						} else {
							this.sureMachine = true
						}
						this.datas = res.data
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// 部门选择选择器
			onpopupopened(e) {
				queryTreeList().then((res) => {
					if (res.code == 0) {
						// uniapp级联选择器更换key值
						this.departDatas = JSON.parse(JSON.stringify(res.data).replace(/id/g, "value").replace(
							/title/g, "text"))
						this.departDatas.forEach((item) => {
							let children = JSON.parse(JSON.stringify(item.children).replace(/id/g, "value")
								.replace(/title/g, "text"))
						})
					}
				})
			},
			onchange(e) {
				this.formList.depart = e.detail.value[0] + ',' + e.detail.value[1].text
				this.selectChildId = e.detail.value[1].value
				this.selectChildTitle = e.detail.value[1].text
			},
			//获取app权限
			getAppMenuFct() {
				getAppMenu().then((res) => {
					res.basic_machine.map(item => {
						if (item == "basic:machine:transfer") {
							this.transferBtnShow = true
						} else if (item == "basic:machine:scrap") {
							this.scrapBtnShow = true
						} else if (item == "basic:machine:out") {
							this.outBtnShow = true
						} else if (item == "basic:machine:in") {
							this.inBtnShow = true
						}
						console.log(this.transferBtnShow)
					})
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//报废按钮
			scrapOpen() {
				this.$refs.scrapPopup.open()
			},
			scrapConfirm() {
				machineScrap({
					machineId: this.datas.id,
					scrapReason: this.scrapInput,
				}).then((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					uni.redirectTo({
						url: "/pages/HomePage/Machine/MachineManage/index"
					})
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//接收按钮
			sureOpen() {
				this.$refs.surePopup.open()
			},
			sureConfirm() {
				confirmReceive({
					machineLoanId: this.datas.machineLoan.id,
				}).then((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					uni.redirectTo({
						url: "/pages/HomePage/Machine/MachineManage/index"
					})
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//借/还
			submitOpen() {
				this.$refs.submitPopup.open()
			},
			radioChange(e) {
				this.radio = e.detail.value
			},
			submitConfirm() {
				if (this.radio == '1') {
					if (this.datas.machineLoan == null || this.datas.machineLoan.billStatus == '0') {
						uni.showToast({
							title: this.$t('homepage.Machine.engineUnborrow'),
							icon: "none",
							duration: 3000
						})
					} else {
						machineSubmit({
							machineId: this.datas.id,
							billType: this.radio,
						}).then((res) => {
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 3000
							})
							uni.redirectTo({
								url: "/pages/HomePage/Machine/MachineManage/index"
							})
						}).catch((res) => {
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 3000
							})
						});
					}
				} else {
					machineSubmit({
						machineId: this.datas.id,
						billType: this.radio,
					}).then((res) => {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
						uni.redirectTo({
							url: "/pages/HomePage/Machine/MachineManage/index"
						})
					}).catch((res) => {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					});
				}
			},
			//划拨按钮
			transferOpen() {
				if (this.formList.depart) {
					this.$refs.transferPopup.open()
				} else {
					uni.showToast({
						title: this.$t('homepage.Machine.selectDepart'),
						icon: "none",
						duration: 3000
					})
				}
			},
			transferConfirm() {
				console.log(this.datas.id, this.selectChildId, this.selectChildTitle)
				transfer({
					machineId: this.datas.id, //机器id
					deptId: this.selectChildId, //部门id
					deptName: this.selectChildTitle, //部门名称
				}).then((res) => {
					console.log(res)
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					uni.redirectTo({
						url: "/pages/HomePage/Machine/MachineManage/index"
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.formList {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 10rpx;
		padding-bottom: 10rpx;
		margin-bottom: 110rpx;
	}

	.formText {
		color: #999;
	}

	.zy_btn_group {
		width: 100%;
		position: fixed;
		bottom: 0px;
		background-color: #f5f5f5;
	}

	.btn {
		width: 25%;
	}
</style>